﻿Imports System.Net.Mail
Imports System.Text.RegularExpressions

Namespace Utilities
    Public Class Mail
        Private mSmtpServer, mEmailUsername, mEmailPassword, mEmailAddress As String
        Private mFrom, mTo, mCC, mBCC, mSubject, mBody As String

        Public Property SmtpServer() As String
            Get
                Return mSmtpServer
            End Get
            Set(ByVal Value As String)
                mSmtpServer = Value
            End Set
        End Property

        Public Property EmailUsername() As String
            Get
                Return mEmailUsername
            End Get
            Set(ByVal Value As String)
                mEmailUsername = Value
            End Set
        End Property

        Public Property EmailPassword() As String
            Get
                Return mEmailPassword
            End Get
            Set(ByVal Value As String)
                mEmailPassword = Value
            End Set
        End Property

        Public Property EmailAddress() As String
            Get
                Return mEmailAddress
            End Get
            Set(ByVal Value As String)
                mEmailAddress = Value
            End Set
        End Property

        Public Property From() As String
            Get
                Return mFrom
            End Get
            Set(ByVal Value As String)
                mFrom = Value
            End Set
        End Property

        Public Property [To]() As String
            Get
                Return mTo
            End Get
            Set(ByVal Value As String)
                mTo = Value
            End Set
        End Property
        Public Property CC() As String
            Get
                Return mCC
            End Get
            Set(ByVal Value As String)
                mCC = Value
            End Set
        End Property
        Public Property BCC() As String
            Get
                Return mBCC
            End Get
            Set(ByVal Value As String)
                mBCC = Value
            End Set
        End Property

        Public Property Subject() As String
            Get
                Return mSubject
            End Get
            Set(ByVal Value As String)
                mSubject = Value
            End Set
        End Property

        Public Property Body() As String
            Get
                Return mBody
            End Get
            Set(ByVal Value As String)
                mBody = Value
            End Set
        End Property

        Public Sub New()
            mSmtpServer = vbNullString
            mEmailUsername = vbNullString
            mEmailPassword = vbNullString
            mEmailAddress = vbNullString
            mFrom = vbNullString
            mTo = vbNullString
            mCC = vbNullString
            mBCC = vbNullString
            mSubject = vbNullString
            mBody = vbNullString
        End Sub

        Public Sub New(ByVal SmtpServer As String, ByVal EmailAddress As String)
            mSmtpServer = SmtpServer
            mEmailAddress = EmailAddress
        End Sub

        Public Sub New(ByVal SmtpServer, ByVal EmailUsername, _
                        ByVal EmailPassword, ByVal EmailAddress)
            mSmtpServer = SmtpServer
            mEmailUsername = EmailUsername
            mEmailPassword = EmailPassword
            mEmailAddress = EmailAddress
        End Sub

        Public Function Send(ByVal From As String, ByVal [To] As String, ByVal Subject As String, ByVal Body As String) As Boolean
            mFrom = From
            mTo = [To]
            mSubject = Subject
            mBody = Body
            Return Send()
        End Function

        Public Function Send(ByVal [To] As String, ByVal Subject As String, ByVal Body As String, Optional ByVal UseSSL As Boolean = False, Optional ByVal CC As String = "", Optional ByVal BCC As String = "", Optional ByVal vTypeID As Integer = 0)
            mFrom = mEmailAddress
            mTo = [To]
            mSubject = Subject
            mBody = Body
            mCC = CC
            mBCC = BCC
            Return Send(UseSSL)
        End Function

        Public Function Send(Optional ByVal UseSSL As Boolean = False) As Boolean
            Dim msg As New MailMessage()
            Dim smtp As New SmtpClient()
            Dim strPatten As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
            Try
                msg.SubjectEncoding = System.Text.Encoding.UTF8
                msg.BodyEncoding = System.Text.Encoding.UTF8
                msg.IsBodyHtml = True
                Select Case Trim(From)
                    Case "webmaster@meotom.net"
                        msg.From = New MailAddress(From, "WebMaster[at]meotom.net")
                    Case "service@vnexpress.net"
                        msg.From = New MailAddress(From, "VnExpress.Net")
                    Case Else
                        msg.From = New MailAddress(From)
                End Select
                msg.Subject = Subject
                msg.Body = Body

                If Not [To].Length < 1 Then
                    [To] = [To].Replace(";", ",").Replace(">", "").Replace("<", "")
                    CC = CC.Replace(";", ",").Replace(">", "").Replace("<", "")
                    BCC = BCC.Replace(";", ",").Replace(">", "").Replace("<", "")

                    msg.To.Add([To])

                    If Not CC.Length < 1 Then
                        For Each strMailCC As String In Regex.Split(CC, ",")
                            If strMailCC.Length > 1 And Regex.IsMatch(strMailCC, strPatten) Then
                                msg.CC.Add(strMailCC)
                            End If
                        Next
                    End If
                    If Not BCC.Length < 1 Then
                        For Each strMailBCC As String In Regex.Split(BCC, ",")
                            If strMailBCC.Length > 1 And Regex.IsMatch(strMailBCC, strPatten) Then
                                msg.Bcc.Add(strMailBCC)
                            End If
                        Next
                    End If

                    smtp.Host = SmtpServer
                    smtp.Credentials = New System.Net.NetworkCredential(EmailUsername, EmailPassword)
                    If UseSSL Then
                        smtp.Port = 465
                        smtp.EnableSsl = True
                    End If
                    smtp.Send(msg)
                    Return True
                Else
                    Return False
                End If
            Catch ex As Exception
                Return False
            Finally
                smtp = Nothing
                msg = Nothing
            End Try
        End Function
    End Class
End Namespace